package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.RecipeCategoryDetail;
import org.dtrd.modules.food.entity.request.RecipeCategoryRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwRecipeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 食谱分类 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-08
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class RecipeCategoryController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwRecipeCategoryService recipeCategoryService;

    @ApiOperation(value = "查询食谱分类详情", response = RecipeCategoryDetail.class)
    @GetMapping("1001612")
    @AutoLog(value="查询食谱分类详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getRecipeCategory(@RequestParam Integer id){
        RecipeCategoryDetail info = recipeCategoryService.getRecipeCategory(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询食谱分类列表", response = RecipeCategoryDetail.class)
    @PostMapping("1001611")
    @AutoLog(value="查询食谱分类列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getRecipeCategoryList(@RequestBody RecipeCategoryRequest request){
        List<RecipeCategoryDetail> list = recipeCategoryService.getRecipeCategoryList(request);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改食谱分类")
    @PostMapping("1001613")
    @AutoLog(value="新增或修改食谱分类", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateRecipeCategory(@RequestBody RecipeCategoryDetail dto){
        if (StrUtil.isBlank(dto.getCategoryName())) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = recipeCategoryService.saveOrUpdateRecipeCategory(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除食谱分类")
    @DeleteMapping("1001614")
    @AutoLog(value="删除食谱分类", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeRecipeCategory(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = recipeCategoryService.removeRecipeCategory(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
